<?php
define("EW_PAGE_ID", "edit", TRUE); // Page ID
define("EW_TABLE_NAME", 'contact', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "contactinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('contact');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanEdit()) {
	$Security->SaveLastUrl();
	Page_Terminate("contactlist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$contact->Export = @$_GET["export"]; // Get export parameter
$sExport = $contact->Export; // Get export parameter, used in header
$sExportFile = $contact->TableVar; // Get export file, used in header
?>
<?php

// Load key from QueryString
if (@$_GET["c_id"] <> "") {
	$contact->c_id->setQueryStringValue($_GET["c_id"]);
}

// Create form object
$objForm = new cFormObj();
if (@$_POST["a_edit"] <> "") {
	$contact->CurrentAction = $_POST["a_edit"]; // Get action code
	LoadFormValues(); // Get form values
} else {
	$contact->CurrentAction = "I"; // Default action is display
}

// Check if valid key
if ($contact->c_id->CurrentValue == "") Page_Terminate($contact->getReturnUrl()); // Invalid key, exit
switch ($contact->CurrentAction) {
	case "I": // Get a record to display
		if (!LoadRow()) { // Load Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
			Page_Terminate($contact->getReturnUrl()); // Return to caller
		}
		break;
	Case "U": // Update
		$contact->SendEmail = TRUE; // Send email on update success
		if (EditRow()) { // Update Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "Update successful"; // Update success
			Page_Terminate($contact->getReturnUrl()); // Return to caller
		} else {
			RestoreFormValues(); // Restore form values if update failed
		}
}

// Render the record
$contact->RowType = EW_ROWTYPE_EDIT; // Render as edit
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "edit"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_c_dateadd"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Date Add"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_c_dateadd"];
		if (elm && !ew_CheckEuroDate(elm.value)) {
			if (!ew_OnError(elm, "Incorrect date, format = dd/mm/yyyy - Date Add"))
				return false; 
		}
		elm = fobj.elements["x" + infix + "_c_status"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Status"))
				return false;
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript" src="fckeditor/fckeditor.js"></script>
<script type="text/javascript">
<!--
_width_multiplier = 16;
_height_multiplier = 60;
var ew_DHTMLEditors = [];

function ew_UpdateTextArea() {
	if (typeof ew_DHTMLEditors != 'undefined' &&
		typeof FCKeditorAPI != 'undefined') {			
			var inst;			
			for (inst in FCKeditorAPI.__Instances)
				FCKeditorAPI.__Instances[inst].UpdateLinkedField();
	}
}

//-->
</script>
<link rel="stylesheet" type="text/css" media="all" href="calendar/calendar-win2k-1.css" title="win2k-1" />
<script type="text/javascript" src="calendar/calendar.js"></script>
<script type="text/javascript" src="calendar/lang/calendar-en.js"></script>
<script type="text/javascript" src="calendar/calendar-setup.js"></script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Edit TABLE: contact<br><br><a href="<?php echo $contact->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<form name="fcontactedit" id="fcontactedit" action="contactedit.php" method="post">
<p>
<input type="hidden" name="a_edit" id="a_edit" value="U">
<table class="ewTable">
	<tr class="ewTableRow">
		<td class="ewTableHeader">Contact ID</td>
		<td<?php echo $contact->c_id->CellAttributes() ?>><span id="cb_x_c_id">
<div<?php echo $contact->c_id->ViewAttributes() ?>><?php echo $contact->c_id->EditValue ?></div>
<input type="hidden" name="x_c_id" id="x_c_id" value="<?php echo ew_HtmlEncode($contact->c_id->CurrentValue) ?>">
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Full Name</td>
		<td<?php echo $contact->c_fullname->CellAttributes() ?>><span id="cb_x_c_fullname">
<input type="text" name="x_c_fullname" id="x_c_fullname"  size="30" maxlength="250" value="<?php echo $contact->c_fullname->EditValue ?>"<?php echo $contact->c_fullname->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Email</td>
		<td<?php echo $contact->c_email->CellAttributes() ?>><span id="cb_x_c_email">
<input type="text" name="x_c_email" id="x_c_email"  size="30" maxlength="250" value="<?php echo $contact->c_email->EditValue ?>"<?php echo $contact->c_email->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Address</td>
		<td<?php echo $contact->c_address->CellAttributes() ?>><span id="cb_x_c_address">
<input type="text" name="x_c_address" id="x_c_address"  size="30" maxlength="250" value="<?php echo $contact->c_address->EditValue ?>"<?php echo $contact->c_address->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Telephone</td>
		<td<?php echo $contact->c_tel->CellAttributes() ?>><span id="cb_x_c_tel">
<input type="text" name="x_c_tel" id="x_c_tel"  size="30" maxlength="250" value="<?php echo $contact->c_tel->EditValue ?>"<?php echo $contact->c_tel->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Subject</td>
		<td<?php echo $contact->c_subject->CellAttributes() ?>><span id="cb_x_c_subject">
<input type="text" name="x_c_subject" id="x_c_subject"  size="30" maxlength="250" value="<?php echo $contact->c_subject->EditValue ?>"<?php echo $contact->c_subject->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Content</td>
		<td<?php echo $contact->c_content->CellAttributes() ?>><span id="cb_x_c_content">
<textarea name="x_c_content" id="x_c_content" cols="35" rows="20"<?php echo $contact->c_content->EditAttributes() ?>><?php echo $contact->c_content->EditValue ?></textarea>
<script type="text/javascript">
<!--
var editor = new ew_DHTMLEditor("x_c_content");
editor.create = function() {
	var sBasePath = 'fckeditor/';
	var oFCKeditor = new FCKeditor('x_c_content', 35*_width_multiplier, 20*_height_multiplier);
	oFCKeditor.BasePath = sBasePath;
	oFCKeditor.ReplaceTextarea();
	this.active = true;
}
ew_DHTMLEditors[ew_DHTMLEditors.length] = editor;
-->
</script>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Date Add<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $contact->c_dateadd->CellAttributes() ?>><span id="cb_x_c_dateadd">
<input type="text" name="x_c_dateadd" id="x_c_dateadd"  value="<?php echo $contact->c_dateadd->EditValue ?>"<?php echo $contact->c_dateadd->EditAttributes() ?>>
&nbsp;<img src="images/ew_calendar.gif" id="cx_c_dateadd" alt="Pick a Date" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup(
{
inputField : "x_c_dateadd", // ID of the input field
ifFormat : "%d/%m/%Y", // the date format
button : "cx_c_dateadd" // ID of the button
}
);
</script>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Status<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $contact->c_status->CellAttributes() ?>><span id="cb_x_c_status">
<?php
$arwrk = $contact->c_status->EditValue;
if (is_array($arwrk)) {
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($contact->c_status->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " checked" : "";
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="radio" name="x_c_status" id="x_c_status" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $contact->c_status->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
	</tr>
</table>
<p>
<input type="button" name="btnAction" id="btnAction" value="   Edit   " onClick="ew_SubmitForm(this.form);">
</form>
<script type="text/javascript">
<!--
ew_CreateEditor();  // Create DHTML editor(s)

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $contact;
	$contact->c_id->setFormValue($objForm->GetValue("x_c_id"));
	$contact->c_fullname->setFormValue($objForm->GetValue("x_c_fullname"));
	$contact->c_email->setFormValue($objForm->GetValue("x_c_email"));
	$contact->c_address->setFormValue($objForm->GetValue("x_c_address"));
	$contact->c_tel->setFormValue($objForm->GetValue("x_c_tel"));
	$contact->c_subject->setFormValue($objForm->GetValue("x_c_subject"));
	$contact->c_content->setFormValue($objForm->GetValue("x_c_content"));
	$contact->c_dateadd->setFormValue($objForm->GetValue("x_c_dateadd"));
	$contact->c_dateadd->CurrentValue = ew_UnFormatDateTime($contact->c_dateadd->CurrentValue, 7);
	$contact->c_status->setFormValue($objForm->GetValue("x_c_status"));
}

// Restore form values
function RestoreFormValues() {
	global $contact;
	$contact->c_id->CurrentValue = $contact->c_id->FormValue;
	$contact->c_fullname->CurrentValue = $contact->c_fullname->FormValue;
	$contact->c_email->CurrentValue = $contact->c_email->FormValue;
	$contact->c_address->CurrentValue = $contact->c_address->FormValue;
	$contact->c_tel->CurrentValue = $contact->c_tel->FormValue;
	$contact->c_subject->CurrentValue = $contact->c_subject->FormValue;
	$contact->c_content->CurrentValue = $contact->c_content->FormValue;
	$contact->c_dateadd->CurrentValue = $contact->c_dateadd->FormValue;
	$contact->c_dateadd->CurrentValue = ew_UnFormatDateTime($contact->c_dateadd->CurrentValue, 7);
	$contact->c_status->CurrentValue = $contact->c_status->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $contact;
	$sFilter = $contact->SqlKeyFilter();
	if (!is_numeric($contact->c_id->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@c_id@", ew_AdjustSql($contact->c_id->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$contact->Row_Selecting($sFilter);

	// Load sql based on filter
	$contact->CurrentFilter = $sFilter;
	$sSql = $contact->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$contact->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $contact;
	$contact->c_id->setDbValue($rs->fields('c_id'));
	$contact->c_fullname->setDbValue($rs->fields('c_fullname'));
	$contact->c_email->setDbValue($rs->fields('c_email'));
	$contact->c_address->setDbValue($rs->fields('c_address'));
	$contact->c_tel->setDbValue($rs->fields('c_tel'));
	$contact->c_subject->setDbValue($rs->fields('c_subject'));
	$contact->c_content->setDbValue($rs->fields('c_content'));
	$contact->c_dateadd->setDbValue($rs->fields('c_dateadd'));
	$contact->c_status->setDbValue($rs->fields('c_status'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $contact;

	// Call Row Rendering event
	$contact->Row_Rendering();

	// Common render codes for all row types
	// c_id

	$contact->c_id->CellCssStyle = "";
	$contact->c_id->CellCssClass = "";

	// c_fullname
	$contact->c_fullname->CellCssStyle = "";
	$contact->c_fullname->CellCssClass = "";

	// c_email
	$contact->c_email->CellCssStyle = "";
	$contact->c_email->CellCssClass = "";

	// c_address
	$contact->c_address->CellCssStyle = "";
	$contact->c_address->CellCssClass = "";

	// c_tel
	$contact->c_tel->CellCssStyle = "";
	$contact->c_tel->CellCssClass = "";

	// c_subject
	$contact->c_subject->CellCssStyle = "";
	$contact->c_subject->CellCssClass = "";

	// c_content
	$contact->c_content->CellCssStyle = "";
	$contact->c_content->CellCssClass = "";

	// c_dateadd
	$contact->c_dateadd->CellCssStyle = "";
	$contact->c_dateadd->CellCssClass = "";

	// c_status
	$contact->c_status->CellCssStyle = "";
	$contact->c_status->CellCssClass = "";
	if ($contact->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($contact->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($contact->RowType == EW_ROWTYPE_EDIT) { // Edit row

		// c_id
		$contact->c_id->EditCustomAttributes = "";
		$contact->c_id->EditValue = $contact->c_id->CurrentValue;
		$contact->c_id->CssStyle = "";
		$contact->c_id->CssClass = "";
		$contact->c_id->ViewCustomAttributes = "";

		// c_fullname
		$contact->c_fullname->EditCustomAttributes = "";
		$contact->c_fullname->EditValue = ew_HtmlEncode($contact->c_fullname->CurrentValue);

		// c_email
		$contact->c_email->EditCustomAttributes = "";
		$contact->c_email->EditValue = ew_HtmlEncode($contact->c_email->CurrentValue);

		// c_address
		$contact->c_address->EditCustomAttributes = "";
		$contact->c_address->EditValue = ew_HtmlEncode($contact->c_address->CurrentValue);

		// c_tel
		$contact->c_tel->EditCustomAttributes = "";
		$contact->c_tel->EditValue = ew_HtmlEncode($contact->c_tel->CurrentValue);

		// c_subject
		$contact->c_subject->EditCustomAttributes = "";
		$contact->c_subject->EditValue = ew_HtmlEncode($contact->c_subject->CurrentValue);

		// c_content
		$contact->c_content->EditCustomAttributes = "";
		$contact->c_content->EditValue = ew_HtmlEncode($contact->c_content->CurrentValue);

		// c_dateadd
		$contact->c_dateadd->EditCustomAttributes = "";
		$contact->c_dateadd->EditValue = ew_HtmlEncode(ew_FormatDateTime($contact->c_dateadd->CurrentValue, 7));

		// c_status
		$contact->c_status->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Active");
		$contact->c_status->EditValue = $arwrk;
	} elseif ($contact->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$contact->Row_Rendered();
}
?>
<?php

// Update record based on key values
function EditRow() {
	global $conn, $Security, $contact;
	$sFilter = $contact->SqlKeyFilter();
	if (!is_numeric($contact->c_id->CurrentValue)) {
		return FALSE;
	}
	$sFilter = str_replace("@c_id@", ew_AdjustSql($contact->c_id->CurrentValue), $sFilter); // Replace key value
	$contact->CurrentFilter = $sFilter;
	$sSql = $contact->SQL();
	$conn->raiseErrorFn = 'ew_ErrorFn';
	$rs = $conn->Execute($sSql);
	$conn->raiseErrorFn = '';
	if ($rs === FALSE)
		return FALSE;
	if ($rs->EOF) {
		$EditRow = FALSE; // Update Failed
	} else {

		// Save old values
		$rsold =& $rs->fields;
		$rsnew = array();

		// Field c_id
		// Field c_fullname

		$contact->c_fullname->SetDbValueDef($contact->c_fullname->CurrentValue, NULL);
		$rsnew['c_fullname'] =& $contact->c_fullname->DbValue;

		// Field c_email
		$contact->c_email->SetDbValueDef($contact->c_email->CurrentValue, NULL);
		$rsnew['c_email'] =& $contact->c_email->DbValue;

		// Field c_address
		$contact->c_address->SetDbValueDef($contact->c_address->CurrentValue, NULL);
		$rsnew['c_address'] =& $contact->c_address->DbValue;

		// Field c_tel
		$contact->c_tel->SetDbValueDef($contact->c_tel->CurrentValue, NULL);
		$rsnew['c_tel'] =& $contact->c_tel->DbValue;

		// Field c_subject
		$contact->c_subject->SetDbValueDef($contact->c_subject->CurrentValue, NULL);
		$rsnew['c_subject'] =& $contact->c_subject->DbValue;

		// Field c_content
		$contact->c_content->SetDbValueDef($contact->c_content->CurrentValue, NULL);
		$rsnew['c_content'] =& $contact->c_content->DbValue;

		// Field c_dateadd
		$contact->c_dateadd->SetDbValueDef(ew_UnFormatDateTime($contact->c_dateadd->CurrentValue, 7), ew_CurrentDate());
		$rsnew['c_dateadd'] =& $contact->c_dateadd->DbValue;

		// Field c_status
		$contact->c_status->SetDbValueDef($contact->c_status->CurrentValue, 0);
		$rsnew['c_status'] =& $contact->c_status->DbValue;

		// Call Row Updating event
		$bUpdateRow = $contact->Row_Updating($rsold, $rsnew);
		if ($bUpdateRow) {
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$EditRow = $conn->Execute($contact->UpdateSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($contact->CancelMessage <> "") {
				$_SESSION[EW_SESSION_MESSAGE] = $contact->CancelMessage;
				$contact->CancelMessage = "";
			} else {
				$_SESSION[EW_SESSION_MESSAGE] = "Update cancelled";
			}
			$EditRow = FALSE;
		}
	}

	// Call Row Updated event
	if ($EditRow) {
		$contact->Row_Updated($rsold, $rsnew);
	}
	$rs->Close();
	return $EditRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
